Method and apparatus for rendering a computer generated image

ABSTRACT

A method and apparatus for rendering a computer-generated image using a stencil buffer is described. The method divides an arbitrary closed polygonal contour into first and higher level primitives, where first level primitives correspond to contiguous vertices in the arbitrary closed polygonal contour and higher level primitives correspond to the end vertices of consecutive primitives of the immediately preceding primitive level. The method reduces the level of overdraw when rendering the arbitrary polygonal contour using a stencil buffer compared to other image space methods. A method of producing the primitives in an interleaved order, with second and higher level primitives being produced before the final first level primitives of the contour, is described which improves cache hit rate by reusing more vertices between primitives as they are produced.

This is a divisional of prior U.S. application Ser. No. 12/800,540,filed May 17, 2010.

FIELD OF INVENTION

The invention relates to a method and apparatus for rendering computergenerated images, in which the images include at least one closedpolygonal contour. Particularly, the invention relates to rendering ofimages using image space calculations and standard graphics hardware andin which the polygons in the images include ‘arbitrary’ shaped polygons,where ‘arbitrary’ permits the existence concavities, self-intersections,and even multiple ‘contours’.

BACKGROUND OF THE INVENTION

In a computer-generated image, there are typically a large number ofindividual polygons. Graphics rendering hardware, in particular 3Dgraphics hardware, often only has capability for the rendering oftriangle primitives or, occasionally, other convex polygons, that is tosay, polygons in which all the internal angles of the polygon are lessthan 180°. Such polygons are relatively straightforward to render. Suchspecifications include ‘fill rules’ which determine which parts of anarbitrary polygon are to be deemed interior and which are exterior. SVGdefines two such rules—‘even-odd’ and “non-zero”. For brevity in thisdocument, we will usually assume use of the ‘even-odd’ rule but it willbe clear to one skilled in the art that the techniques presented applyto other well-defined fill rules.

FIGS. 1 a to 1 e show some examples of ‘arbitrary’ polygons (includingconcave polygons, polygons with self-intersections and multiple contourpolygons). FIGS. 1 a and 1 e each show an example of a concave polygon,that is to say, a polygon in which at least one of the internal anglesis greater than 180°. FIG. 1 b shows an example of a polygon with selfintersections, that is to say, a polygon in which not every part of eachline segment between two vertices remains inside or on the boundary ofthe polygon. FIG. 1 c shows an example of a polygon with multiplecontours, that is to say, a polygon with a hole requiring an externaland an internal contour to define the overall shape. FIG. 1 d shows anexample of a polygon including all these features.

The ability to render such arbitrary polygons, whilst also supportingconvex polygons, is useful for a number of reasons, for example, tosupport vector graphics standards such as SVG (Scalable Vector Graphics)and OpenVG (Open Vector Graphics). SVG is a language for describingtwo-dimensional graphics and graphical applications in XML (ExtensibleMarkup Language). OpenVG is a royalty-free, application programminginterface (API) designed for hardware-accelerated 2-dimensional vectorgraphics. Naturally, any method able to render the arbitrary polygons,must also be able to handle convex polygons.

There are two families of methods with the capability of renderingarbitrary polygons on such hardware. The first family performscalculations in model space and are generally referred to astriangulation algorithms. These take a polygon outline and produce a setof non-overlapping triangles that exactly cover the filled area of theoriginal polygon. To avoid confusion with other uses of “triangulation”in this document, we will refer to such algorithms as “truetriangulation”. An example of the possible results of such a process, asapplied to the polygon of FIG. 1 a is shown in FIG. 2. The originalshape can thus be constructed from the triangles: {[v2, v3, v5], [v3,v4, v5], [v5, v6, v2], [v6, v2, v7], [v7, v2, v1]}. Assuming a simplepolygon with N-sides and that no extra vertices are added (note somealgorithms do introduce additional vertices), we will obtain N−2triangles. Once these triangles are generated, they can easily berendered on any commodity graphics hardware.

Numerous algorithms for the “true triangulation” process have beenpublished. Lamot and Zalik provide a survey of methods in “An overviewof triangulation algorithms for simple polygons” (InformationVisualization, 1999, pp 153-158). These documented methods are nearlyalways restricted to simple polygons such as FIGS. 1( a) and (e), i.e.,they may contain neither self-intersections (including repeatedvertices) nor multiple contours. Nevertheless, Meister's “ear cutting”(or ear clipping) algorithm and Seidel's method are of interest to thisdiscussion.

Meister's method removes one vertex at a time from a (simple) polygon insuch a way that the reduced polygon remains simple. It repeatedly ‘clipsan ear’, formed by a triple of consecutive vertices, from the polygon.This algorithm runs in O(n³) time and, although it has been subsequentlyimproved to be O(n²), it is not particularly attractive except forpolygons with relatively few vertices.

Seidel's method, on the other hand, runs in O(n log*n) time for simplepolygons where log*(n) is defined as . . .

${\log^{*}n} = \left\{ \begin{matrix}0 & {{{if}\mspace{14mu} n} \leq 1} \\{1 + {\log^{*}\left( {\log \; n} \right)}} & {{{if}\mspace{14mu} n} > 1}\end{matrix} \right.$

We can thus consider O(n log*n) to be practically O(n) for anyreasonable values of n.

As stated above, very few ‘true triangulation’ algorithms have beenpublished that handle arbitrary polygons. Held's method (“FIST: FastIndustrial-Strength Triangulation of Polygons” Algorithmica 30, 4,563-596) is one of the few exceptions. Although based on ear clipping,additional structures are used to achieve a much better time complexityfor simple polygons, but it is not clear to the inventor how it behavesin the presence of self-intersections etc.

The application's inventor has implemented a version of Seidel'salgorithm that has been enhanced to support completely arbitrarypolygons. This still achieves virtually linear performance (assuming theimplicit vertices created by self-intersections are included in ‘n’).However, on a ˜2 GHz CPU, the process still takes an average of 1˜2 μsper polygon vertex. For polygons that will be drawn multiple times overnumerous frames, the triangulation results can be cached, and so thepre-processing cost is amortised by the rendering process. Forsituations, however, where a polygon is only drawn a few times or isbeing dynamically altered on a frame-by-frame basis—which forcesre-triangulation—the true triangulation process can be a verysignificant penalty. (Note that applying linear transformations to themodel does not require re-triangulation.)

The second family of methods with the capability of rendering arbitrarypolygons uses image space calculations. Here the rendering/samplingprocess itself is adapted to determine which pixels fall inside thearbitrary polygon and which are outside. Although this can be done withscan line rendering algorithms, we are primarily interested in thosethat break the arbitrary polygon into smaller polygons (usuallytriangles) for which the hardware has direct rendering support, renderthose smaller polygons and make use of the hardware stencil buffer todetermine which of the rendered pixels are inside the original arbitrarypolygon. It is well known in the art (“OpenGL programming guide: theofficial guide to learning OpenGL, version 1.4”, Shreiner et al, ISBN0321173481) that arbitrary polygons can drawn by using the stencilbuffer. For example, one may implement the odd-even rule by applying XORoperations to the stencil. Similarly, provided triangle winding ordersare taken into account, increments and decrements of the stencil can beused to implement the non-zero rule.

With either fill rule, one must first produce a set of triangles fromthe source polygon. The obvious approach is described in the “DrawingFilled, Concave Polygons Using the Stencil Buffer” section of chapter 13of Shreiner et al (available at eitherhttp://fly.cc.fer.hr/˜unreal/theredbook/chapter13.html or http://www.scribd.com/doc/7605395/Redbook). Here a triangle fan (referChapter 2 of Shreiner et al or http://en.wikipedia.org/wiki/Trianglefan) is created by simply submitting the vertices in order, i.e. [v₁,v₂, v₃, . . . v_(N)] which implicitly creates the set of N−2 triangleswith vertices {[v₁, v₂, v₃], [v₁, v₃, v₄], [v₁, v₄, v₅] . . . [v₁,v_(N-1), v_(N)]}.

Borrowing the example (FIG. 1 (a)) from Shreiner et al, part of thisprocess is shown in FIG. 3. The seven sided figure is rendered as a fanof five triangles. Assuming the odd-even fill rule, the pixels of thescreen which are covered by an odd number of triangles will be deemedinterior while those covered by an even number will be exterior. Forexample, the area bounded by v₁, v3, and location 20, is covered bytriangles [v₁, v₂, v₃] and [v₁, v₃, v₄]. Assuming that the stencilbuffer is initialised to zero and an XOR operation employed, drawingtriangle [v₁, v₂, v₃] will first set all the pixels' stencil values forregion [v₁, v₃, “20”] but these will subsequently be cleared again bytriangle [v₁, v₃, v₄]. The region will thus be correctly deemed exteriorto the polygon. The simplicity of this process is extremely appealingand, since it uses a triangle fan, it only requires the transmission ofN vertices to the graphics hardware.

Once the stencil has been set to indicate which pixels are inside thepolygon, it must be filled with the appropriate colours or textures.Methods to do this include computing the 2D bounding rectangle of allthe vertices of the polygon and then drawing just a single rectangle(with stencil test), or to simply resend the generated triangles. Theformer, as applied to FIG. 1( a) and illustrated in FIG. 15( a), has theadvantage of sending a near minimal amount of geometric data to thehardware but requires pre-computation of min and max bounds. It also canbe expensive, in terms of wasted pixel processing, if the rectangle doesnot tightly bound the polygon to be filled, as shown by the region 415.

Another method, as shown in FIG. 15( b),—in this example using a set oftriangles generated using the invention's method (refer FIG. 7)—sendsmore geometry than the bounding box method but generally results in lessredundant pixel filling. In this example, much of the shape is filledwith a single ‘layer/pass’ of pixels, 450, but there are regions wherepixels are filled multiple times, 460. This typically becomes worse withpolygons with greater numbers of regions of, or total area of,concavity.

The method also works unaltered for self-intersecting and multiplecontour polygons—Shreiner et al also provide an example of the latter.In effect they just concatenate all the vertices of all the contours andtreat the result as larger triangle fan.

Despite the pleasing simplicity of this fan method, as described in theart, the inventor has appreciated that it has two fundamental problems.The first is related to the shape of the generated triangles. Producinga fan of triangles from the original polygon tends to lead to theproduction of long, thin triangles. Such a triangle is generally slowerto render with graphics hardware than another that has an equal screenarea but is ‘more equilateral’ in shape. One publication, “Silhouetteclipping”, (Sander et al, SIGGRAPH 2000, pages 327-334) discusses thisproblem and gives a partial solution. Sander et al also need to fillsets of contour edges. These are, in effect, polygons and are likely tohave concavities. They state:

-   -   “The basic algorithm described so far tends to draw many long,        thin triangles. On many rasterizing chips (e.g. NVIDIA's TNT2),        there is a large penalty for rendering such eccentric triangles.        It is easy to show that the setStencil algorithm behaves best        when the screen-space projection of q has a y coordinate at the        median of the contour vertices. Choosing q as the 3D centroid of        the contour vertices serves as a fast approximation”.        . . . and . . .    -   “Each edge contour is drawn as a fan of triangles about an        arbitrary center point, which we choose to be the 3D centroid of        the contour vertices.”

This typically does improve the shape of the triangles but unfortunatelyintroduces an extra point, which thus requires the data to be readtwice. It also creates an additional triangle in the fan. An example ofthe results of their process, as applied to FIG. 1 (a), is shown in FIG.4. (Please note that the ‘centroid’ location, Vcentroid, is only anapproximation in this illustration).

-   -   Sander et al suggest a further improvement:    -   “To further reduce the eccentricity of the fan triangles, we        break up each large contour into a set of smaller loops. More        precisely, we pick two vertices on the contour, add to the data        structure two opposing directed edges between these vertices,        and proceed as before on the smaller loops thus formed.”

This is, unfortunately, quite vague. Firstly, they don't say how “wepick two vertices”. Secondly, in the context of the paper, “proceed asbefore on the smaller loops” would appear to imply the process ofcomputing the ‘centroid’ of each loop and turning each into a fan. Thatdoes not seem correct as it would only produce two child loops.

A more likely interpretation is that they have a target, M, for thenumber of vertices per child ‘loop’ and divide the source polygon intosufficient equal pieces to meet that target number. An N-vertex sourcepolygon would thus require P child polygons where P=└N/(M−1)┘. Withtheir scheme, if the source polygon is thus divided into P sections,then P additional vertices (each located at the centroid of itsrespective ‘loop’) are introduced. It should be noted that, since eachchild loop is drawn with a fan, there are practical reasons—described inthe following paragraph—for not choosing too small a value for M.

Also of relevance to the invention are the methods by which contemporaryrendering hardware reduces the triangle data and bus bandwidth whenmodels are supplied to the rendering hardware. The simplest method is tosupply each triangle as three, V-byte vertices so that, for a model withT triangles, 3*T*V bytes of data would be transmitted to the hardware,but more efficient options exist. We have already seen that 3D hardwaretypically supports the concept of triangle fans, whereby a T trianglefan only needs to supply (T+2)*V bytes of data. For 3D models, a relatedconcept called triangle strips (again see Shreiner orhttp://en.wikipedia.org/wiki/Triangle strip), is typically more useful.Like triangle fans, these also require only (T+2)*V bytes of data for astrip of T triangles. In both cases, the ratio of triangles to verticesclimbs asymptotically towards 1:1 as the length of the strip or fanincreases. Longer strips and fans are thus more efficient.

Over the past decade, an indexed triangle format has been seeingincreased popularity as a means of further decreasing the bandwidth andstorage costs. Here each triangle is defined as three integer indices,each say of 16 or 32 bits, which select vertices from a vertex array.With 3D models, this format offers the opportunity to exceed the 1:1barrier of strips and fans, though this is unlikely for 2D shapes. Toefficiently support such a format, graphics hardware now frequentlyemploys a vertex caching technique such as that described by Hoppe(“Optimization of mesh locality for transparent vertex caching”,Computer Graphics (SIGGRAPH '99 Proceedings) pp 269-276). In brief, thehardware maintains a cache of the last K vertices used in pasttriangles. A FIFO replacement policy is generally employed rather than,say, a Least Recently Used (LRU) scheme as the former is not onlysimpler but, more importantly, generally results in a higher cache hitrate for 3D models.

We now return to the second, and probably far more significant problemwith the prior art fan algorithm, which is that it can require adisproportionate amount of “pixel filling”. For example, one can seefrom FIG. 3 that there is a relatively large area which is covered bymultiple triangles compared to the ideal situation of FIG. 2 asgenerated by a ‘real triangulation’ method. We will refer to the areascovered by multiple triangles as ‘overdraw’. This overdraw is anundesirable burden in the rendering phase and it is advantageous toreduce it if possible.

On average, simply using the less-obvious triangle strip order i.e.outputting the vertices in the order v₁, v₂, v_(N), v₃, v_(N-1), v₄ . .. and thus producing the triangles {[v₁, v₂, v_(N)], [v₂, v_(N), v₃],[v_(N), v₃, v_(N-1)] . . . } often results in both better shapedtriangles and lower overdraw compared to fan order (although,ironically, not in the particular case FIG. 1 (a)), but the improvementis unfortunately not that great. From FIG. 4, Sander et al's methodwould also appear to reduce overdraw at the expense of introducing anadditional vertex and triangle, but it certainly does not work in allcases. Applying their method to FIG. 1 (e), where the centroid would belocated in the centre of the “U”, would result in significant regions ofoverdraw, as shown in FIG. 5.

The inventor has appreciated that there is a need for a method ofproducing a set of simpler polygons (usually, but not always, triangles)from an arbitrary polygon for rendering with a stencil buffer methodwhich:

-   -   Avoids pre-processing of the polygon data—(if the pre-processing        becomes expensive one might as well use a true triangulation        algorithm.)    -   Does not introduce additional vertices.    -   On average, produces lower overdraw rates than the fan (or        strip) methods.    -   On average, produces ‘more equilateral’ shaped triangles than        the fan/strip methods.    -   Is simple to implement in both software and hardware and uses        relatively few operations. It must, of course, be O(n).        It is an object of the present invention to provide a method and        system that goes some way towards achieving the above goals.

In addition, for any method and system, the following features, thoughnot necessarily essential, are desired:

-   -   Geometry data transfer costs that are approximately equivalent        with the fan/strip methods.    -   The supplied primitives, e.g. triangles, should, preferably, be        arranged in an order so that “chronologically close” primitives        are also close in screen space so that caching (e.g. frame        buffer caching) can be more effective.    -   A method should not be substantially more complex than the fan        method.

SUMMARY OF THE INVENTION

The invention is defined in the appended claims to which referenceshould now be made. Advantageous features are defined in the dependentclaims.

In order to address the issues identified with known methods, theinventor has appreciated the following:

-   -   A vertex on a polygon more typically forms a convex, rather than        concave, angle.    -   The support of indexed triangles in rendering hardware may allow        alternative triangle orders to be supported efficiently.    -   Vertices with numerically local indices tend to also be        spatially local. This is typically more apparent with polygons        with larger numbers of vertices such as those illustrated in        FIGS. 6 a, 6 b, and 6 c.    -   Graphics rendering hardware nearly always provides ‘free’        support for determining if a polygon is clockwise or        anticlockwise and can be instructed to cull out polygons with a        particular winding order.

In the “ear clipping algorithms” (e.g. Meister or Held) for truetriangulation, a ‘safe’ vertex is identified and then removed to reducean N sided shape to an N−1 shape. The clipped vertex then forms atriangle with its two original neighbours. Unfortunately, theidentification of a ‘safe’ vertex, i.e. where the produced triangle isentirely inside the original polygon is expensive.

We have appreciated that if one is using an image space method andrendering using a stencil buffer, then it not absolutely critical if thevertex selected is ‘safe’. Taking the example from FIG. 3, one canconsider that the fan method, in effect, progressively clips the earsformed by vertices V₂, V₃, . . . V₅ until the polygon is reduced to thetriangle {V₁, V₆, V₇}.

For triangle primitives, the inventor has appreciated that if a simpleear clipping algorithm is applied to every second vertex of an original(single) contour of an N-sided polygon, the └N/2┘ triangles thus formedwill typically be more equilateral in shape than those produced by thefan or strip method. Furthermore, these triangles are less likely tohave large regions that either lie outside of the final filled shape oroverlap other triangles, than those typically generated by the fan orstrip methods. After this process, one will be left with a ‘residual’polygon with ┌N┐ (ie ceiling(N/2)) vertices. The same process may thenbe reapplied to produce another set of triangles and another residualpolygon with even fewer vertices. This process is thus repeated untilthe residual polygon is ‘empty’ i.e. when the residual polygon is aprimitive of the size desired for output or the residual polygon hastrivially zero area. The results of such a process as applied to FIG. 1(a) are shown in FIG. 7. In this example, the desired result of loweroverdraw has been achieved.

The preceding paragraph assumes that the rendering system supportstriangle primitives. Some rendering systems also support the renderingof quads (either convex or arbitrary) or even M-sided ‘arbitrary’polygons with say, M≦16. Although the preferred embodiments detailedbelow demonstrate the invention for triangle primitives, the inventionis not restricted to the output of triangle primitives and may outputM-sided primitive units, or output primitive units of more than onetype, eg triangles and quads, if used with more flexible renderingsystems. In the presently preferred embodiments, one type of primitive,e.g. triangles, will be outputted to cover the majority of the contour.

It is also highly desirable to make the colour or texture fillingprocess that occurs once the stencil has been set up, be as efficient asreasonably possible. To this end, the inventor has also appreciated thata single contour arbitrary polygon, without self-intersections, willhave an overall winding order and, furthermore, it is usuallyadvantageous for this winding order to be consistent for all objectsdrawn by an application. (This is due to the ‘triangle fill’ or‘tie-breaking rules’ of graphics hardware as, say, summarised by theOpenGL, DirectX, or OpenVG standards, to avoid artefacts such as ‘gaps’appearing between abutting objects). For practical systems, a multiplecontour arbitrary polygon will thus also use a consistent overallwinding order for the other pieces. (By consistent, it should be notedthat contours representing ‘holes’ will have the opposite winding orderand that each contour will not, in itself, be self-intersecting.)

The inventor has noted that, once the stencil buffer has been set up,for such an arbitrary polygon that has an overall winding order, W, onlythose triangles that were used to create the stencil that also havewinding order W will be needed when filling the object. This can bedemonstrated by considering the behaviour of the non-zero fill rule asthe pixels drawn are a superset of those produced by the odd-even rule.

Assuming the overall winding order W corresponds to an increment of apixel's stencil value, we thus will only need to fill those pixels whosestencil has a positive value. Triangles with the opposite winding orderonly subtract from the stencil's value and, since we are assuming aconsistent winding order, can be eliminated. Since rendering pipelinestypically provide free support for eliminating polygons with a userselectable winding order, the original triangulation can be re-used.(Note that polygons such as a self-intersecting ‘bow-tie’. i.e.“[0,038], [1,0], [0,1], [1,1]” do not have an overall winding order andso cannot use this additional optimisation).

This process, as applied to the triangulation of FIG. 1 (a) asillustrated in FIG. 7, is shown in FIG. 16, and can be compared with theresults shown in FIG. 15( b). One can see that the triangle, 520, can beeliminated from the fill process, leaving, in this case, just regionswith a single layer of fill, 510. There is still some redundant filling,but this is greatly reduced compared to 460.

BRIEF DESCRIPTION OF THE DRAWINGS

Exemplary embodiments of the invention will now be described in detailwith reference to the accompanying drawings, in which:

FIGS. 1 a to 1 e show five examples of ‘arbitrary’ polygons;

FIG. 2 shows the possible results of applying a ‘true triangulation’process to the polygon of FIG. 1 a;

FIG. 3 shows triangles generated by the most well known, prior artprocess for rendering in conjunction with a stencil buffer as applied tothe polygon of FIG. 1 a;

FIG. 4 shows an additional point and triangles generated using Sander etal's method as applied to the polygon of FIG. 1 a;

FIG. 5 shows Sander et al's method as applied to the polygon of FIG. 1e;

FIGS. 6 a to 6 e show five examples of ‘arbitrary’ polygons with moresignificant numbers of vertices;

FIG. 7 shows the triangles generated by one embodiment of the presentinvention as applied to the polygon of FIG. 1 a;

FIG. 8 shows the first step of processing of an embodiment of theinvention, in which a multi-contoured arbitrary polygon is logicallysplit into its contours;

FIGS. 9 a to 9 f each show a subset of the triangles, each setcorresponding to a certain hierarchical “size” of triangle, as generatedby an embodiment of the invention as applied to the polygon of FIG. 6 a;

FIG. 10 shows a subset (every 10^(th) triangle) of the output of amodified fan method applied to the polygon of FIG. 6 a;

FIGS. 11 a and 11 b illustrate areas of overdraw that occur with thetriangles generated by an embodiment of the invention, FIG. 11 a asapplied to the polygon of FIG. 6 a, and FIG. 11 b the overdraw caused bya modified fan algorithm;

FIG. 12 shows an overview of an exemplary embodiment of the apparatus ofthe invention;

FIGS. 13 a and 13 b show a pathological polygon case for an embodimentof the invention;

FIGS. 14 a and 14 b show an alternate configuration of the case of FIGS.13 a and 13 b, that is not pathological;

FIG. 15 shows two methods of filling the polygon's pixels once thestencil buffer values have been determined. Method (A) uses arectangular bounding box determined from the extremities of the polygonwhile method (B) resends the triangulation used to create the stencil;and

FIG. 16 shows the result of filling the polygon's pixels once thestencil buffer has been determined for a polygon with a consistentwinding order, using the triangulation generated to create the stencilbut where triangles with the opposite winding order have beeneliminated.

DETAILED DESCRIPTION OF A PREFERRED EMBODIMENT

A first preferred embodiment is essentially that described earlier whichis represented by the steps:

1. An arbitrary polygon is first logically separated into itsconstituent closed contours or sub-paths. FIG. 8 shows this simpleprocess applied to the example polygon of FIG. 1( d). Clearly, for asingle contour polygons such as those of FIGS. 1( a), (b) and (e), thisis step can be ignored.

Note that this differs from Shreiner et al where the contours are allconcatenated and treated as a single fan. Although simple, it usuallyleads to significantly greater overdraw.

2. A contour is converted to a set of primitives, in this casetriangles, with the method described below, and the resulting primitivese.g. triangles are concatenated with those sets generated by any earliercontours of the polygon.

3. The combined sets of primitives e.g. triangles are rendered using thestencil buffer.

The conversion of a contour, C⁰=(v₀ ⁰, v₁ ⁰, v₂ ⁰, . . . v_(N) ₀ ₋₁ ⁰)where N⁰≧3, to a set of primitives, consists of dividing the closedpolygonal contour into smaller polygonal units of maximum size P, P≧3,vertices by repeatedly removing (up to) a first set of P−2 vertices fromthe contour, to produce a polygon with P vertices, (v₀ ⁰, v₁ ⁰, . . .v_(P-1) ⁰), and a reduced source contour with N⁰−(P−2) vertices,removing (if possible) a second set of (up to) P−2 vertices startingwith the last vertex of previous set and continuing thus until the endof the source contour. More precisely, this produces an initial set ofsmaller polygons,

{(v ₀ ⁰ ,v ₁ ⁰ . . . v _(P-1) ⁰),(v _(P-1) ⁰ ,v _(P) ⁰ . . . v _(2P-2)⁰) . . . (v _(i.(P-1)) ⁰ ,v _(i.(P-1)) ⁰ . . . v _(N) ₀ ₋₁ ⁰)}

. . . and a residual contour . . .

C ¹=(v ₀ ⁰ ,v _(P-1) ⁰ ,v _(2.(P-1)) ⁰ , . . . v _(N) ₀ ₋₁ ⁰)

. . . which, for convenience of expression, can be renumbered as . . .

$C^{1} = {{\left( {v_{0}^{1},v_{1}^{1},{\ldots \mspace{14mu} v_{N^{1} - 1}^{1}}} \right)\mspace{14mu} {where}\mspace{14mu} N^{i + 1}} = \left\lfloor \frac{N^{i}}{P} \right\rfloor}$

This process is repeated with each subsequently reduced contour, C^(j),until it has either P or fewer vertices, or been reduced to a linesegment (and thus trivially zero area). The generated primitives arethen rendered using a stencil buffer to produce a computer-generatedimage.

For example, the process of step 2 applied to a single contour, togenerate triangles (ie for P=3), in accordance with a first embodimentof the invention will now be described. It is assumed that the contourhas N-sides with vertices numbered from Base to Base+N−1:

StepSize := 1; WHILE( StepSize < N ) {   // generate all the triangleswith this step size   J := 0;   WHILE (J < (N − StepSize))   {    // Getthe other two points that will make    // up this triangle    PtB :=MIN(J+ StepSize, N−1);    PtC := MIN(J + 2*StepSize, N−1);    //if thisis a non degenerate triangle...    IF(PtB != PtC)    {     //outputtriangle {J, PtB, PtC} relative     //to “base”     OutputTriangle(J +Base,            PtB + Base,            PtC + Base);    }    // Move tothe next triangle in this set    J := J + 2*StepSize;   }   // Doublethe step size   StepSize := StepSize * 2; }It should be appreciated that this simple algorithm can be realised inhardware by one skilled in the art using a language such as VHDL orVerilog and would require only a small number of components includingadders, registers, and comparators.

FIG. 6 shows five examples of arbitrary polygons with more significantnumbers of vertices. The map of Australia in FIG. 6 a containsapproximately 1500 vertices with 26 contours. The ‘Hilbert curve’ shownin FIG. 6 b contains 1000 vertices. The “Th” text in FIG. 6 c has 807vertices, while both the random walk, shown in FIG. 6 d, and star, shownin FIG. 6 e, both have 100 source vertices. Any of these arbitrarypolygons may be rendered using the invention. For example, FIG. 9 showscertain subsets of the output triangles of the method, as applied to thepolygon of FIG. 6 a, wherein the six images show the results ofcapturing and rendering the triangle output when StepSize is 1, 4, 16,64, 256, and 512 respectively. For comparison purposes, FIG. 10 showsevery 10^(th) triangle generated by a modified prior-art fan algorithm(the modification being to treat each contour separately rather than theless efficient merging as suggested by Shreiner et al). Comparing FIG. 9and FIG. 10 one can see that triangles thus produced by the inventionare, for this source model, significantly better shaped than thosegenerated by the fan algorithm.

Of more importance is the amount of overdraw produced by the invention.FIG. 11 (A) shows the pixels that are covered by more than one triangle.For simplicity, this figure does not distinguish between areas coveredby two triangles and those covered by three or more triangles eventhough the latter clearly is increasingly costly. Nevertheless, if wejust count the number of pixels touched by the overdraw region relativeto the pixels of the final intended result, we find that overdraw has anarea that is approximately 15% of the area of the intended final result.

By contrast, FIG. 11 (B) shows the overdraw area using the modified fanalgorithm. In this situation, the overdrawn pixels represent a regionthat, surprisingly, has an area that is approximately 39% of the finalresult. Note that the unmodified Shreiner fan algorithm would besignificantly worse.

This first embodiment of the invention described above does, on average,address the two most important issues, i.e. that of improved triangleshape and reduced overdraw. Though of less importance, the firstembodiment does not achieve the triangle data efficiency of fans andstrips, whereby an N-sided contour, requiring N−2 triangles, can betransmitted with just N vertices. Another potential drawback of themethod of the first embodiment is that, with the possible exception ofthe final larger values of StepSize, generated triangles that are‘chronologically’ local, are often only spatially local in the vicinityof a single shared vertex.

If one examines the order of vertices created by the previouslydescribed embodiment, (and assuming the vertices start from “1”) one cansee that the triangles are created in the order {[v₁, v₂, v₃], [v₃, v₄,v₅], [v₅, v₆, v₇] . . . [v₁, v₃, v₅], [v₅, v₇, v₉] . . . }. If we assumethis data is provided in an indexed format to hardware with a K-vertexcache (where K is typically much smaller than N), it will only achieve acache hit rate of around 33% since only one vertex is usually reusedbetween adjacent triangles. A fan or strip, on the other hand, achievesnearly 66% as two vertices are shared.

A second preferred embodiment will now be presented that addresses theseadditional issues. This second embodiment incorporates the additionalfeature of a very small stack. Assuming the maximum number of verticesin a contour that will ever be encountered is N_(Max), then this stackneeds at most ┌ log₂(N_(Max))┐+1 (ie ceiling(log₂(N_(max)))+1) entries.For example, if in a given system a contour could have at most 2¹⁶vertices, then the stack would have no more than 17 entries.

This embodiment provides interleaving of primitive levels to maximisevertex temporal locality and produces at least one second levelprimitive before all the first level primitives are produced. Primitivepolygon level is reflected in the separation of the vertices of theprimitive polygon relative to the vertices of the original arbitraryclosed polygonal contour. A first level primitive has vertices which arecontiguous vertices in the original closed polygonal contour. Higherlevel primitives have vertices which are offset from each other in theoriginal closed polygonal contour. The amount of offset is related tothe primitive level and the number of vertices in the primitive. An(i+1)^(th) level (i≧1) primitive is formed of end points of consecutivei^(th) level primitives. Considering the set of i^(th) level primitives,and associated vertices, each vertex will belong to exactly one memberof the set except for the special case of end points, an end point beingshared with at most one other i^(th) level primitive.

When a contour is divided into a single size of primitive over allprimitive levels, for primitive level i and polygon primitive size Q,the vertices in the i^(th) level primitive correspond to vertices offsetfrom each other by (Q−1)̂(i−1) in the original arbitrary closed polygonalcontour. For example, when dividing a contour using only triangleprimitives, assuming first level primitives have vertices [v1, v2, v3],[v3, v4, v5] . . . , the second level primitives will have vertices [v1,v3, v5], [v5, v7, v9] . . . with an offset of (3−1)̂(2−1)=2 relative tothe vertices of the original closed polygonal contour, and the thirdlevel primitives will have vertices [v1, v5, v9], [v9, v13, v17] . . .with an offset of 4 relative to the vertices of the original closedpolygonal contour etc. . . .

The second embodiment uses the same steps 1 and 3 as the first variant,but replaces step 2, i.e. the triangulation of a single contour, withthe following method, as expressed in pseudo code. We will assume, asbefore, that the contour starts at vertex number ‘base’ and has Nvertices.

int Vstack[MAX_VERTEX_STACK_SIZE]; //vertex stack int StackDepth; intCurrentVertexID; // put the first vertex, 0, on the stack.StackDepth  := 1; Vstack[0] := 0; CurrentVertexID := 1; //next vertex toprocess*/ // while we have at least 2 more verticesWHILE(CurrentVertexID <= N−2) {   // put the next two vertices on thestack   Vstack[StackDepth]  := CurrentVertexID;   Vstack[StackDepth+1]:= CurrentVertexID+1;   CurrentVertexID+=2;   StackDepth +=2;   // forma triangle from the top 3 vertices  OutputTriangle(Vstack[StackDepth−3] + Base,         Vstack[StackDepth−2] + Base,          Vstack[StackDepth−1] +Base);   // remove the ‘second from top’ stack element  Vstack[StackDepth−2] := Vstack[StackDepth−1];   StackDepth−−;   // doall the higher triangle levels we can..   WHILE((StackDepth >= 3) &&   ((Vstack[StackDepth−1] − Vstack[StackDepth−2]) ==    (Vstack[StackDepth−2] − Vstack[StackDepth−3])) )   {    // form atriangle from the top 3 vertices   OutputTriangle(Vstack[StackDepth−3] + Base,          Vstack[StackDepth−2] + Base,           Vstack[StackDepth−1] +Base);    // remove the second from top stack element   Vstack[StackDepth−2] := Vstack[StackDepth−1];    StackDepth−−;  }//end while doing upper levels }//end while at least 2 vertices left// process remaining whole triangles on the stack WHILE(StackDepth >= 3){   // form a triangle from the top 3 vertices  OutputTriangle(Vstack[StackDepth−3] + Base,         Vstack[StackDepth−2] + Base,          Vstack[StackDepth−1] +Base);   // remove the second from top stack element  Vstack[StackDepth−2] := Vstack[StackDepth−1];   StackDepth−−; }//endwhile // if there is just one vertex left to do, // add it to the stackand form the final triangle IF(CurrentVertexID <= N−1) {  Vstack[StackDepth] := CurrentVertexID;   StackDepth++;   // form atriangle from the top 3 vertices   OutputTriangle(Vstack[StackDepth−3] +Base,          Vstack[StackDepth−2] + Base,         Vstack[StackDepth−1] + Base); }// end if one leftover vertex

In this embodiment, the triangles are produced in an order which is farmore ‘vertex cache friendly’. Specifically, the triangles produced are .. .

{[v ₁ ,v ₂ ,v ₃ ],[v ₃ ,v ₄ ,v ₅ ],[v ₁ ,v ₃ ,v ₅ ],[v ₅ ,v ₆ ,v ₇ ],[v₇ ,v ₈ ,v ₉ ],[v ₅ ,v ₇ ,v ₉ ],[v ₁ ,v ₅ ,v ₉] . . . }.

In essence, triangles corresponding to various values of “StepSize” ofthe first embodiment are interleaved. Assuming N is sufficiently large,triangles are produced corresponding to the following levels, at leastinitially, with the pattern . . .

StepSize=[1,1,2,1,1,2,4,1,1,2,1,1,2,4,8,1 . . . ]

With the ordering produced by the second embodiment and, assuming theexistence of a 16-entry vertex cache with a FIFO replacement policy, thehit rate for, say, a 120 vertex contour is a respectable 62% which isnearly double that of the first embodiment and on-par with a fan orstrip.

An example of an apparatus implementing the invention is shown in FIG.12. The parameters for a contour are supplied, 200, to an input buffer,210. These are read by a state machine, 220, which implements the stepsdescribed in the pseudo code above.

The state machine has access to a vertex stack. The vertex stack ispreferably split into two portions, a first portion 230 containing Pstack entries and a second portion 240. The state machine 220 has directaccess to the top P entries (in the case of triangulation, P=3) of thefirst portion of the vertex stack 230. Because it is desirable to haveparallel read and write access to these three entries, these wouldpreferably be implemented as independent registers. The second portionof the vertex stack 240 holds the remaining stack entries and would onlyrequire a single read/write port, and so could be implemented with acheaper register file with an index to the element that is 4^(th)highest on the stack.

There is a read/write path, 250, between the first portion of the vertexstack 230 containing in this case the top three entries, and theremainder of the stack space, second portion 240, to allow for pushingand popping of stack data.

Unit 230 also supports the ability to remove or overwrite centralelements (when P=3, the second from top stack element, corresponding tothe elements which will be culled or clipped as the contour isprocessed) as described in the pseudo code above. The stack operationsare done under the guidance of the state machine. The primitive outputunit, here a triangle output unit, 260, can be instructed by the statemachine to select the top three stack elements, 230, and output thecorresponding triangle.

Using a rendering simulator, the “cycle” counts for filling of thestencil for the arbitrary polygons of FIG. 6, using a number of thedescribed triangle generation techniques are compared against eachother. Also, as a target benchmark, the rendering cycles (not includingpre-processing costs) of the results of a true triangulation algorithmare supplied. For ease of interpretation, the scores are normalised sothat the (modified) fan algorithm scores 1.0. Smaller figures arebetter. The “Sander” algorithm is the inventor's interpretation ofSander et al's document.

method Model Fan* Strip “Sander” Invention Adapted Seidels Map (J) 1.01.04 0.45 0.36 0.24 Hilbert (K) 1.0 0.55 0.36 0.32 0.06 Text (L) 1.00.71 0.84 0.83 0.20 Rand Walk (M) 1.0 0.97 0.62 0.68 0.10 Star (N) 1.01.21 0.77 0.53 0.18As can be seen, then invention generally compares favourably against theother stencil-based methods.

The second preferred embodiment can further be adapted so that it isunnecessary to know, in advance, how many vertices are in a particularcontour before starting to generate the triangle indices. Such anembodiment would be useful in an application which streams vertices thatare generated ‘on the fly’. Additionally, it can be modified to notrequire support for indexed triangles in the rendering hardware by alsostoring vertex coordinates in a wider stack.

Some rendering systems also support the rendering of quads (eitherconvex or arbitrary) or even M-sided ‘arbitrary’ polygons with, say,M≦16. Either of the presented preferred embodiments can be easilyadapted, without departing from the scope of the invention, to outputprimitive units other than triangles to suit these more flexiblerendering systems.

The invention thus presented, on average, reduces overdraw, improves thetriangle shape and/or reduces the data required, relative to the priorart, but one can encounter pathological situations. A very simple caseis shown in FIG. 13 (A). Here, the first three vertices, {v1, v2, v3},form a concavity in the shape, as do all the other “StepSize=1”triangles, 300, as shown in grey in FIG. 13 (B). After their processing,the method effectively still has to fill the large pentagonal region,310. As can be seen from the figure, all these triangles would form aconsiderable area of overdraw.

The location of the first vertex in FIG. 13 was ‘unfortunate’. If theembodiments, instead, received the geometrically equivalent figure shownin FIG. 14 (A), the alternative “Stepsize=1” triangles, 350 in FIG. 14(B), would instead be produced. This would leave just the region, 370,to be covered by the remaining “stepsize” triangles and lead to nooverdraw at all.

Another embodiment thus attempts to reduce the frequency of thesepathological cases. Taking inspiration from Seidel's ‘truetriangulation’ method, the alternative embodiment uses a randomisationtechnique. For each contour, a random or pseudo random offset value canbe supplied to or computed by the embodiment. This offset is then added,modulo N, to all the vertex indices in the contour to reduce thelikelihood of recurring worst cases.

In some polygon data, the vertices themselves may be supplied as anarray of arbitrary indices into a vertex array. It should be obvious oneskilled in the art that the embodiments presented here can be extendedto support such indirection.

In the Adobe flash format, the edges of arbitrary polygons areapparently supplied in a random, disconnected order. One skilled in theart will realise that a hashing system can be used to reorder these intoconnected chains before applying the invention described here.

Once the stencil has been set up by drawing the triangulation by themethods above, they can be filled/shaded/textured by any of the knownmethods known in the art such as those described above, i.e. using thebounding box or re-use of the triangulation. Furthermore, if the windingorder of the parent polygon is consistent, the additional enhancement,presented in this invention, may be used whereby the triangulation isresent to the rendering system but instructed to cull triangles with theopposite winding order.

What is claimed is:
 1. A method for rendering a computer generated imageusing a stencil buffer, comprising: receiving an arbitrary closedpolygonal contour, the contour having N vertices; dividing the arbitraryclosed polygonal contour into primitives, each primitive being a polygonhaving at least 3 and at most P vertices where 2<P<N; and using astencil buffer to render the primitives to produce a computer generatedimage; wherein dividing comprises: i) selecting a vertex of thearbitrary closed polygonal contour as a first index vertex and settingthe source contour as the arbitrary closed polygonal contour; ii)outputting a primitive of Q vertices using the index vertex and (Q−1)contiguous vertices of the source contour; iii) forming a reduced sourcecontour by removing from the source contour the (Q−2) vertices betweenthe index vertex and the end vertex of the outputted primitive; iv)setting the end vertex of the outputted primitive as the index vertex;v) repeating steps (ii) to (iv) until all vertices of the source contourhave been included in at least one of the generated primitives; vi)selecting a vertex of the reduced source contour as an index vertex andsetting the source contour as the reduced source contour; vii) repeatingsteps (ii) to (v) until either the reduced source contour is outputtedas a primitive or until the reduced source contour has trivially zeroarea.
 2. A method according to claim 1, wherein an offset is addedmodulo N to all the vertex indices in the arbitrary closed polygonalcontour prior to dividing the contour into primitives.
 3. A methodaccording to claim 1, wherein using the stencil buffer to render theprimitives further comprises comparing the winding order of eachprimitive to the overall winding order of the arbitrary closed polygonalcontour and using only those primitives whose winding order is the sameas the overall winding order in the colour and/or texture fillingprocess.
 4. A method according to claim 1, wherein a complex arbitrarypolygon comprising multiple closed polygonal contours is received, themethod additionally comprising separating the complex arbitrary polygoninto its constituent closed polygonal contours, division into primitivesthen proceeding on each constituent closed polygonal contour and theresulting primitives of each constituent closed contour beingconcatenated before rendering using the stencil buffer.
 5. A methodaccording to claim 1, wherein substantially all primitives aretriangles.
 6. Apparatus for rendering a computer generated imagecomprising an input for receiving an arbitrary closed polygonal contour;means for dividing the arbitrary N vertex closed polygonal contour intoprimitives, each primitive being a polygon having at least 3 and at mostP vertices where 2<P<N, and outputting the primitives; and a stencilbuffer for rendering the primitives to produce a computer generatedimage; wherein the means for dividing the arbitrary closed polygonalcontour into primitives is configured to implement the method ofclaim
 1. 7. Apparatus according to claim 10, wherein the means fordividing the arbitrary closed polygonal contour into primitivescomprises: vertex stack means comprising first and second portions, thefirst portion containing P stack entries, the vertex stack meansconfigured to allow pushing and popping of stack data between the firstand second portions; primitive output means, coupled to the firstportion of the vertex stack means and to state machine means, foroutputting primitives from the data on the first portion of the vertexstack means; state machine means, coupled to the vertex stack means,configured to manage the data on the first and second portions of thevertex stack means to provide interleaving of primitive levels in theoutput of the primitive output means.
 8. Apparatus for rendering acomputer generated image comprising: an input for receiving an arbitraryclosed polygonal contour; means for dividing the arbitrary closedpolygonal contour into primitives, each primitive being a polygon havingat least 3 and up to P vertices, and outputting the primitives; astencil buffer, coupled to the means for dividing the arbitrary closedpolygonal contour, for rendering the primitives to produce a computergenerated image; wherein the means for dividing the arbitrary closedpolygonal contour into primitives comprises: vertex stack meanscomprising first and second portions, the first portion containing Pstack entries, the vertex stack means configured to allow pushing andpopping of stack data between the first and second portions; primitiveoutput means, coupled to the first portion of the vertex stack means andto state machine means, for outputting primitives from the data on thefirst portion of the vertex stack means; state machine means, coupled tothe vertex stack means, configured to manage the data on the first andsecond portions of the vertex stack means to provide interleaving ofprimitive levels in the output of the primitive output means.